package com.jingdianjichi.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.jingdianjichi.subject.common.enums.CategoryTypeEnum;
import com.jingdianjichi.subject.common.enums.IsDeletedFlageEnum;
import com.jingdianjichi.subject.domain.convert.SubjectCategoryConverter;
import com.jingdianjichi.subject.domain.convert.SubjectLabelConverter;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBo;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBo;

import com.jingdianjichi.subject.domain.service.SubjectLabelDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.entity.SubjectLabel;

import com.jingdianjichi.subject.infra.basic.entity.SubjectMapping;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService subjectLabelService;
    @Resource
    private SubjectMappingService subjectMappingService;

    @Resource
    private SubjectCategoryService subjectCategoryService;
    public Boolean add(SubjectLabelBo subjectLabelBo) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.add.Bo:{}", JSON.toJSONString(subjectLabelBo));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBo);
        subjectLabel.setIsDeleted(IsDeletedFlageEnum.UN_DELETE.getCode());
        int count = subjectLabelService.insert(subjectLabel);
        return count > 0;
    }





    public Boolean update(SubjectLabelBo subjectLabelBo) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.update.Bo:{}", JSON.toJSONString(subjectLabelBo));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE
                .convertBoToLabel(subjectLabelBo);
        int count = subjectLabelService.update(subjectLabel);

        return count > 0;
    }


    public Boolean delete(SubjectLabelBo subjectLabelBo) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.delete.Bo:{}", JSON.toJSONString(subjectLabelBo));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE
                .convertBoToLabel(subjectLabelBo);
        subjectLabel.setIsDeleted(IsDeletedFlageEnum.DELETE.getCode());
        int count = subjectLabelService.update(subjectLabel);

        return count > 0;

    }

    /**
     * 查询分类下标签
     *
     * @param subjectLabelBo
     * @return
     */

    public List<SubjectLabelBo> queryLabelByCategoryId(SubjectLabelBo subjectLabelBo) {
   //如果单前分类是1级分类，则查询所有标签
     SubjectCategory subjectCategory =   subjectCategoryService.queryById(subjectLabelBo.getCategoryId());
       if (CategoryTypeEnum.PRIMARY.getCode() == subjectCategory.getCategoryType()){
           SubjectLabel subjectLabel = new SubjectLabel();
           subjectLabel.setCategoryId(subjectLabelBo.getCategoryId());
        List<SubjectLabel> LabelList = subjectLabelService.queryByCondition(subjectLabel);
           List<SubjectLabelBo> LabelResultList = SubjectLabelConverter.INSTANCE.convertListBoToLabelBo(LabelList);
       return LabelResultList;
       }

        Long categoryId = subjectLabelBo.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeletedFlageEnum.UN_DELETE.getCode());

        List<SubjectMapping> subjectMappingList = subjectMappingService.queryLabelId(subjectMapping);
        if (CollectionUtils.isEmpty(subjectMappingList)) {
            return Collections.emptyList();
        }
  List<Long> labelIdList=subjectMappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
   List<SubjectLabel> subjectLabelList=  subjectLabelService.batchQueryById(labelIdList);
        List<SubjectLabelBo> boList = new LinkedList<>();
      subjectLabelList.forEach(label->{
          SubjectLabelBo bo = new SubjectLabelBo();
          bo.setId(label.getId());
          bo.setLabelName(label.getLabelName());
          bo.setCategoryId(categoryId);
          bo.setSortNum(label.getSortNum());
          boList.add(bo);
      });
        return boList;
    }
}
